<template>
    <div>
        <van-nav-bar id="reset" title="申请列表" left-arrow @click-left="backHandle" fixed z-index="100" right-text="历史记录"
            @click-right="toPage('/applyHistory')" />




        <div class="pageCon fuPage">

            <div class="listBox">
                <div class="listTop">
                    <span>
                        筛选
                    </span>
                    <div>
                        <span></span>
                        <van-popover v-model="showPopover" trigger="click" theme="dark" :actions="actions"
                            @select="onSelect">
                            <template #reference>
                                {{ type == -1 ? '全部申请' : type == 1 ? '亲情就诊' : '加号申请' }}<van-icon name="bars" />
                            </template>
                        </van-popover>
                    </div>
                </div>


                <div class="listMain">
                    <template v-if="applyList.length != 0">
                        <div class="itemBox" v-for="(item, index) in applyList" :key="index">
                            <div class="orderNumTip">
                                申请编号：{{ item.orderNumber }}
                            </div>
                            <div class="line1">
                                <span>{{ item.userName }}
                                    <span>{{ item.payType == 0 ? '免费' : '付费' }}</span> </span>
                                <span>
                                    <span @click="apply(item, 1)" class="applyBtn">通过</span>
                                    <span @click="apply(item, 0)" class="applyBtn applyBtn1">驳回</span>
                                </span>
                            </div>
                            <div>
                                <span>{{ item.doctorName }}的{{ item.orderType == 0 ? '加号' : '亲情就诊' }}申请</span>
                                <span>{{ item.createTime }}</span>
                                <!-- <span>2024-05-21 12:00:00</span> -->
                            </div>
                            <div class="imgBox" v-if="item.imgList != null && item.imgList.length != 0">
                                <span>相关图片</span>
                                <div>
                                    <img :src="ele.imgUrl" alt="" v-for="(ele, eIndex) in item.imgList" :key="eIndex"
                                        @click="checkImg(item.imgList, eIndex)">
                                </div>
                            </div>
                        </div>
                    </template>
                    <div class="emptyBox" v-else>
                        <img src="../../assets/tip_content.png">
                    </div>
                </div>


            </div>
        </div>




        <van-popup v-model="tipShow" round>
            <div class="duiBox">
                <div>
                    {{ applyFlag == 0 ? '驳回原因' : '就诊时间' }}
                </div>
                <div class="inputBox" v-if="applyFlag == 0">
                    <span>原因：</span>
                    <input type="text" v-model="text" placeholder="请输入驳回原因">
                </div>
                <div class="infoLine" v-else>
                    时间：
                    <span @click="showTime = true">{{ applyTime || '请选择' }}<van-icon name="arrow" /></span>
                </div>
                <div class="duiBtn">
                    <div @click="tipShow = false">取消</div>
                    <div @click="tipConfirm">提交</div>
                </div>
            </div>
        </van-popup>


        <van-action-sheet v-model="showTime">
            <van-datetime-picker @confirm="timeConfirm" @cancel="showTime = false" v-model="time" type="datetime"
                title="选择年月日" :min-date="minDate" />
        </van-action-sheet>


    </div>

</template>

<script>
import { ImagePreview } from 'vant';

import { getOrderList, updateOrderStatus } from '../../network/aide'
export default {
    name: 'HomeR',
    data() {
        return {
            isLoading: false,

            type: -1,// -1全部  1 已使用 0 未使用

            oldList: [],//原始数据
            applyList: [],
            showPopover: false,
            actions: [
                {
                    text: '全部',
                    value: -1
                },
                {
                    text: '加号申请',
                    value: 0
                },
                {
                    text: '亲情就诊',
                    value: 1
                }
            ],

            applyFlag: 0,//  1 通过  0 驳回
            tipShow: false,
            showTime: false,
            selectObj: '',//当前操作的 订单
            time: '',
            applyTime: '',//约定就诊时间
            text: '',//拒绝理由
            minDate: new Date()

        }
    },
    mounted() {

    },
    methods: {

        onSelect(action) {
            console.log(action);
            this.type = action.value


            this.applyList = this.oldList.filter(item => {
                if (this.type == -1) {
                    return item
                } else {
                    return item.orderType == this.type
                }
            })

        },
        apply(obj, flag) {
            this.applyTime = ''
            this.text = ''
            this.applyFlag = flag
            this.tipShow = true
            this.selectObj = obj
        },
        timeConfirm() {
            console.log(this.time);
            this.applyTime = this.getNowDate(this.time)
            this.showTime = false
        },

        tipConfirm() {
            if (this.applyFlag == 0 && this.text == '') {
                this.$toast('请输入驳回原因')
                return
            }
            if (this.applyFlag == 1 && this.applyTime == '') {
                this.$toast('请选择就诊时间')
                return
            }

            console.log(this.selectObj);
            console.log(this.text);
            console.log(this.applyTime);
            // return


            var obj = {
                orderId: this.selectObj.orderId,
                status: this.applyFlag,
                dateTime: this.applyTime,
                note: this.text
            }

            updateOrderStatus(obj).then(res => {
                if (res.flag) {
                    this.$toast('审核成功')
                    this.tipShow = false
                    this.getOrderList()
                } else {
                    this.$toast('审核失败')
                }
            })
        },

        toPage(url, obj) {
            if (url == '/applyHistory') {
                this.$router.push(url)
            } else {
                this.$router.push({
                    path: url,
                    query: {
                        userId: obj.userId || 6,
                    },
                });
            }
        },
        backHandle() {
            this.$router.back()
        },

        pullHandle() {

            this.isLoading = true
            this.getOrderList()
        },


        getOrderList() {
            getOrderList({
                userId: localStorage.userId,
                flag: 1
            }).then(res => {
                if (res.flag) {
                    this.isLoading = false
                    res.data = res.data.sort((a, b) => {
                        return b.orderId - a.orderId
                    })
                    this.oldList = JSON.parse(JSON.stringify(res.data))
                    this.applyList = res.data
                }
            })
        },
        getNowDate(time) {
            var myDate = new Date(time);
            var year = myDate.getFullYear();
            var mon = myDate.getMonth() + 1;
            mon = mon < 10 ? '0' + mon : mon
            var date = myDate.getDate();
            date = date < 10 ? '0' + date : date
            var date1 = myDate.getDate() + 1;
            date1 = date1 < 10 ? '0' + date1 : date1
            var hours = myDate.getHours();
            hours = hours < 10 ? '0' + hours : hours
            var minutes = myDate.getMinutes();
            minutes = minutes < 10 ? '0' + minutes : minutes
            var seconds = myDate.getSeconds();
            seconds = seconds < 10 ? '0' + seconds : seconds
            return year + "-" + mon + "-" + date + ' ' + hours + ':' + minutes
        },


        checkImg(list, index) {

            console.log(list);
            var arr = []
            list.forEach(item => {
                arr.push(item.imgUrl)
            })
            console.log(arr);
            ImagePreview({
                images: arr, // 需要预览的图片 URL 数组
                showIndex: true, // 是否显示页码
                closeable: true, // 是否显示关闭图标	
                startPosition: index,
                showIndicators: true
            })
        },



    },
    created() {
        this.getOrderList()
    },
    //计算属性
    computed: {

    },
    filters: {

    },
    watch: {
    }

}

</script>

<style lang='less' scoped>
.pageCon {

    padding: 112px 20px 0 !important;
    box-sizing: border-box;
    position: relative;
    width: 100%;
    height: 100vh;
    background-color: #EEF1F7;
    overflow: hidden;
}


.listBox {
    width: 100%;
    height: 100%;

    overflow-y: scroll;
    border-radius: 20px;
}


.listTop {
    position: sticky;
    top: 0px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    padding: 30px 34px 30px 44px;
    width: 100%;
    font-family: Source Han Sans CN;
    font-weight: 500;
    font-size: 28px;
    color: #333333;
    background: #fff;
    // border-radius: 20px;
    border-bottom: 2px solid #f2f2f2;
}

.listTop>div>span:nth-child(2) {
    margin-left: 30px;
    color: #999999;
}

.itemBox {
    margin-bottom: 20px;
    box-sizing: border-box;
    padding: 44px 30px 44px 44px;
    width: 100%;
    border-bottom: 2px solid #f2f2f2;
    background: #fff;
    border-radius: 20px;
    box-shadow: 0px 2px 6px 0px rgba(51, 51, 51, 0.08);
}

.itemBox>div {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 28px;
    color: #333333;
    line-height: 38px;

}

.itemBox>div:nth-child(3) {
    margin-top: 12px;
}

.itemBox>div:nth-child(3)>span {
    font-size: 28px;
    color: #999999;
}

.line1>span>span:nth-child(1) {
    color: #333;
}

.line1>span>span {
    font-size: 24px;
    color: #999999;
}

.line1>span:nth-child(2) {
    display: flex;
    align-items: center;
    justify-content: center;

}

.applyBtn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 136px;
    height: 48px;
    border-radius: 24px;
    border: 1px solid rgba(27, 178, 106, .5);
    font-family: Source Han Sans CN;
    font-weight: bold;
    font-size: 20px;
    color: #1BB26A !important;
}

.applyBtn1 {
    margin-left: 16px;
    border: 1px solid rgba(255, 22, 22, 0.5);
    color: rgba(255, 22, 22) !important;
}


#reset /deep/ .van-nav-bar__left .van-nav-bar__text,
#reset /deep/ .van-nav-bar__right .van-nav-bar__text,
#reset /deep/ .van-nav-bar__left .van-icon {
    color: #333 !important;
}


.emptyBox {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
}

.emptyBox>img {
    margin-top: 400px;
    width: 374px;
    height: 314px;
}



.infoLine {
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    width: 100%;
    padding: 24px 48px;
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 28px;
    color: #334840;
    line-height: 64px;
}

.infoLine>span {
    font-family: Source Han Sans CN;
    font-weight: 400;
    font-size: 28px;
    color: #999999;
    line-height: 42px;
}


.duiBox {
    position: relative;
    width: 642px;
    // height: 590px;
    background: #FAFAFA;
    border-radius: 20px;
    color: #333333;
    line-height: 64px;
    font-family: Source Han Sans CN;

}

.duiBox>div:first-child {
    padding-top: 24px;
    text-align: center;
    width: 100%;
    font-weight: bold;
    font-size: 34px;

}

.duiBtn {
    // position: absolute;
    // bottom: 0px;
    display: flex;
    align-items: center;
    width: 100%;
    height: 90px;
    border-top: 2px solid #f2f2f2;
}

.duiBtn>div {
    flex: 1;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 28px;
}

.duiBtn>div:last-child {
    border-left: 2px solid #f2f2f2;
}

.inputBox {
    box-sizing: border-box;
    padding: 24px 48px;
    display: flex;
    align-items: center;
    width: 100%;
    font-size: 28px;
}

.inputBox>input {
    width: 60%;
    border: none;
    outline: none;
    background: transparent;
}



.imgBox {
    margin-top: 24px;
    display: flex;
    flex-direction: column;
    align-items: flex-start !important;
    padding-top: 24px;
    border-top: 1px solid #f2f2f2;
    width: 100%;
}

.imgBox>span {
    font-size: 28px;
    color: #999;
}

.imgBox>div>img {
    margin: 16px 16px 16px 0;
    width: 100px;
    height: 100px;
    // background: aqua;
    border-radius: 16px;
}


.orderNumTip {
    margin-bottom: 12px;
    width: 100%;
    font-size: 24px;
    color: #333;
}
</style>